<script setup>
import MenuBox from "@/components/MenuBox.vue";
import FooterView from "@/components/Footer/FooterView.vue";
import { useRouter } from "vue-router";
// 国际化
import { useI18n } from "vue-i18n";
import { onMounted, ref } from "vue";
import axios from "axios";
const I18n = useI18n();
const { locale } = useI18n();
const isLogin = ref(true);

// 修改语言
const changeLang = (val) => {
  console.log("修改", val);
  if (val === "zh") {
    locale.value = "zh-cn";
  } else {
    locale.value = "en-us";
  }
};

const router = useRouter();

// 跳转页面
const jumpTo = (path) => {
  router.push(path);
};

const logout = () => {
  localStorage.removeItem("LOGIN_STATUS_TOKEN");
  localStorage.removeItem("info");
  isLogin.value = true;
  router.replace("/login");
};

const getInfo = async () => {
  try {
    const res = await axios.get(`${apiUrl}/user`, {
      headers: {
        "Authori-zation": `Bearer ${getToken()}`,
      },
    });
    if (res.data.msg === "请登录") {
      if (localStorage.getItem("LOGIN_STATUS_TOKEN") !== null) {
        console.log(2222);
        localStorage.removeItem("LOGIN_STATUS_TOKEN");
        jumpTo("/login");
      }
    } else {
      isLogin.value = false;
    }
  } catch (e) {}
};

let timer = null;
onMounted(() => {
  getInfo();
  timer = setInterval(getInfo, 30000);
});
</script>

<template>
  <div class="topHeader">
    <div class="navigationBox">
      <div class="middle">
        <div class="tabs">
          <div class="item" v-if="isLogin">
            <div class="btn" @click="jumpTo('login')">
              {{ $t("navigation.login") }}
            </div>
            /
            <div class="btn" @click="jumpTo('login')">
              {{ $t("navigation.regist") }}
            </div>
          </div>
          <!-- <div class="item">
                        <div class="btn">
                            <svg t="1733053485835" class="icon" viewBox="0 0 1024 1024" version="1.1"
                                xmlns="http://www.w3.org/2000/svg" p-id="5954" width="14" height="14">
                                <path
                                    d="M876.678238 832.428982h-27.878144V385.429941A319.139071 319.139071 0 0 0 565.712864 68.115356v-29.921569a35.905882 35.905882 0 1 0-71.738785 0v29.921569A319.21205 319.21205 0 0 0 210.88685 385.429941v446.999041h-27.878144a35.905882 35.905882 0 1 0 0 71.811765H457.995218v47.947489a71.738785 71.738785 0 1 0 143.550549 0v-47.947489h275.059492a35.905882 35.905882 0 1 0 0-71.811765z m-99.689908 0H282.625635V385.429941a247.254327 247.254327 0 0 1 247.181347-247.400286 247.254327 247.254327 0 0 1 247.181348 247.400286v446.999041z"
                                    p-id="5955" fill="#515151"></path>
                            </svg>
                        </div>
                    </div> -->
          <div class="item" v-if="locale === 'zh-cn'">
            <div class="btn" @click="isLogined() && jumpTo('/person?tab=order')">
              {{ $t("navigation.order") }}
            </div>
          </div>
          <div class="item" v-if="locale === 'zh-cn'">
            <div class="btn" @click="isLogined() && jumpTo('/person?tab=balance')">
              {{ $t("navigation.balance") }}
            </div>
          </div>
          <div class="item" v-if="locale === 'zh-cn'">
            <div class="btn car" @click="isLogined() && jumpTo('car')">
              <svg
                t="1733055828450"
                class="icon"
                style="margin-right: 5px"
                viewBox="0 0 1028 1024"
                version="1.1"
                xmlns="http://www.w3.org/2000/svg"
                p-id="7174"
                width="14"
                height="14"
              >
                <path
                  d="M332.8 790.528q19.456 0 36.864 7.168t30.208 19.968 20.48 30.208 7.68 36.864-7.68 36.864-20.48 30.208-30.208 20.48-36.864 7.68q-20.48 0-37.888-7.68t-30.208-20.48-20.48-30.208-7.68-36.864 7.68-36.864 20.48-30.208 30.208-19.968 37.888-7.168zM758.784 792.576q19.456 0 37.376 7.168t30.72 19.968 20.48 30.208 7.68 36.864-7.68 36.864-20.48 30.208-30.72 20.48-37.376 7.68-36.864-7.68-30.208-20.48-20.48-30.208-7.68-36.864 7.68-36.864 20.48-30.208 30.208-19.968 36.864-7.168zM930.816 210.944q28.672 0 44.544 7.68t22.528 18.944 6.144 24.064-3.584 22.016-13.312 37.888-22.016 62.976-23.552 68.096-18.944 53.248q-13.312 40.96-33.28 56.832t-49.664 15.872l-35.84 0-65.536 0-86.016 0-96.256 0-253.952 0 14.336 92.16 517.12 0q49.152 0 49.152 41.984 0 20.48-9.728 35.84t-38.4 14.336l-49.152 0-94.208 0-118.784 0-119.808 0-99.328 0-55.296 0q-20.48 0-34.304-9.216t-23.04-24.064-14.848-32.256-8.704-32.768q-1.024-6.144-5.632-29.696t-11.264-58.88-14.848-78.848-16.384-87.552q-19.456-103.424-44.032-230.4l-76.8 0q-15.36 0-25.6-7.68t-16.896-18.432-9.216-23.04-2.56-22.528q0-20.48 13.824-33.792t37.376-12.288l103.424 0q20.48 0 32.768 6.144t19.456 15.36 10.24 18.944 5.12 16.896q2.048 8.192 4.096 23.04t4.096 30.208q3.072 18.432 6.144 38.912l700.416 0zM892.928 302.08l-641.024-2.048 35.84 185.344 535.552 1.024z"
                  p-id="7175"
                  fill="#515151"
                ></path>
              </svg>
              <div class="name">{{ $t("navigation.car") }}</div>
            </div>
          </div>
          <div class="item" v-if="!isLogin">
            <div class="btn" @click="logout">退出</div>
          </div>

          <div class="item">
            <div class="btn" @click="changeLang($t('navigation.changeLang'))">
              {{ $t("navigation.changeLang") }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <MenuBox />
  <router-view />
  <FooterView />
</template>

<style scoped lang="scss">
.navigationBox {
  width: 100%;
  background-color: #eae8e8;
  height: 30px;

  .middle {
    display: flex;
    flex-direction: row-reverse;
    justify-content: flex-start;
  }

  .tabs {
    display: flex;

    .item {
      margin-left: 30px;
      display: flex;
      line-height: 30px;
      height: 30px;

      div {
        line-height: 30px;
      }

      .btn {
        cursor: pointer;

        &:hover {
          color: var(--primary-color);
        }
      }

      .icon {
        margin-top: 6px;
      }

      .car {
        display: flex;
      }
    }
  }
}
</style>
